var fs = require('fs');
var events = require('events');
var url = require('url')
var http = require('http')
var ejs = require('ejs')
var EventEmitter = new events.EventEmitter()

http.createServer(function (req, res) {
    var method = req.method.toLocaleLowerCase();

    res.writeHead(200, {
        'Content-Type': "text/html;charset=utf-8"
    })
    // var da = "我是后台数据的,我也是ejs模板渲染的";
    // var list = ["呵呵哒", "赛瓦软件", 'nodejs'];
    var pathname = url.parse(req.url).pathname
    if (pathname == '/login') {
        ejs.renderFile('./reg.ejs', {
            // pp: pp,
            // h: h
        }, function (err, data) {
            // console.log(data)
            res.end(data)
        })
    }
     else if (pathname == '/dologin' && method == 'get') {
var a=url.parse(req.url, true).query;
        var c =JSON.stringify(a) +'\n'
      
       fs.appendFile('login.txt',c,function(err){
           if(err){
               console.log(err)
           }
           console.log('写入成功')
          
       })
       console.log(c)
    res.end('<script>alert("注册成功");history.back()</script>')


} else if (pathname == '/dologin' && method == 'post') {
    var str = ''
    req.on('data', function (data) {
        str += data
    })
    req.on('end', function (err, dat) {

        fs.appendFile('login.txt', str+'\n', function (err) {
            if (err) {
                console.log(err)
            }
            console.log("写入成功")
        })
    })


    res.end('<script>alert("注册成功");history.back()</script>')


} else {

    ejs.renderFile('./index.ejs', {}, function (err, dat) {
        // console.log(data)
        res.end(dat)
    })
}

}).listen(3000)